package com.eco.fanliapp.admin.controller.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eco.fanliapp.admin.common.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.SyLabelType;
import com.eco.fanliapp.service.ISyLabelTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 标签  controller
 */

@RestController
@RequestMapping("/web/label")
public class SyLabelTypeController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(SyLabelTypeController.class);

    @Autowired
    private ISyLabelTypeService iSyLabelTypeService;


    @GetMapping(value = "syLabelTypeList")
    @ResponseBody
    public ServerResponse list(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer pageSize) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("enable", 1);
        queryWrapper.eq("deleted", 0);
        Page<SyLabelType> page = new Page<>(currentPage, pageSize);
        IPage<SyLabelType> syLabelTypeList = iSyLabelTypeService.page(page, queryWrapper);

        if (syLabelTypeList.getTotal() != 0) {
            return ServerResponse.createBySuccess("查询成功", syLabelTypeList);
        }
        return ServerResponse.createBySuccess("暂无数据", syLabelTypeList);
    }

    @PostMapping("/saveAndUpdate")
    @ResponseBody
    public ServerResponse saveAndUpdate(@RequestBody SyLabelType syLabelType) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("enable", 1);
        queryWrapper.eq("deleted", 0);
        queryWrapper.eq("label_name", syLabelType.getLabelName());
        List<SyLabelType> syLabelTypeList = iSyLabelTypeService.list(queryWrapper);
        if (syLabelTypeList.size() > 1) {
            return ServerResponse.createByErrorMessage("标签名称重名，请重新输入");
        }
        int i = iSyLabelTypeService.insertSyLabelType(syLabelType);
        if (i > 0) {
            return ServerResponse.createBySuccessMessage("保存成功！");
        }
        return ServerResponse.createByErrorMessage("保存失败");
    }

    @PostMapping("/remove")
    @ResponseBody
    public ServerResponse remove(Integer id) {
        SyLabelType labelType = new SyLabelType();
        labelType.setTableId(id.longValue());
        labelType.setDeleted(1);
        int i = iSyLabelTypeService.removeSyLabelType(labelType);
        if (i > 0) {
            return ServerResponse.createBySuccessMessage("标签删除成功！");
        }
        return ServerResponse.createByErrorMessage("标签删除失败");
    }
}
